﻿// CProcessDlg.cpp: 实现文件
//

#include "pch.h"
#include "Injector.h"
#include "CProcessDlg.h"
#include "afxdialogex.h"


// CProcessDlg 对话框

IMPLEMENT_DYNAMIC(CProcessDlg, CDialogEx)

CProcessDlg::CProcessDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_PROCESS_DIALOG, pParent)
{

}

CProcessDlg::~CProcessDlg()
{
}

void CProcessDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST_PROCESS, m_ProcessList);
}


BEGIN_MESSAGE_MAP(CProcessDlg, CDialogEx)
    ON_NOTIFY(NM_DBLCLK, IDC_LIST_PROCESS, &CProcessDlg::OnNMDblclkListProcess)
END_MESSAGE_MAP()


// CProcessDlg 消息处理程序


BOOL CProcessDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    m_ProcessList.InsertColumn(0, _T("进程名"), LVCFMT_CENTER, 150);
    m_ProcessList.InsertColumn(1, _T("PID"), LVCFMT_CENTER, 50);
    m_ProcessList.InsertColumn(2, _T("父进程"), LVCFMT_CENTER, 50);
    m_ProcessList.InsertColumn(3, _T("线程数"), LVCFMT_CENTER, 50);
    m_ProcessList.InsertColumn(4, _T("优先级"), LVCFMT_CENTER, 50);
    m_ProcessList.InsertColumn(5, _T("路径"), LVCFMT_LEFT, 500);
    m_ProcessList.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_FLATSB);

    InitializeProcessList();

    return TRUE;
}


void CProcessDlg::InitializeProcessList()
{
    HANDLE hSnapShot = INVALID_HANDLE_VALUE;
    TCHAR FilePath[MAX_PATH] = { 0 };
    PROCESSENTRY32 pe32 = { 0 };
    CString str;
    int nItem = 0;
    HANDLE hProcess;

    do
    {
        //
        //  打开进程快照句柄
        //

        hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        if (INVALID_HANDLE_VALUE == hSnapShot) break;

        //
        //  获取第一个进程信息
        //

        pe32.dwSize = sizeof(PROCESSENTRY32);
        if (FALSE == Process32First(hSnapShot, &pe32)) break;

        //
        //  初始化进程列表
        //

        do
        {
            hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
            GetModuleFileNameEx(hProcess, NULL, FilePath, MAX_PATH);
            if (hProcess == NULL) 
                wsprintf(FilePath, _T("System Path"));
            CloseHandle(hProcess);

            str = pe32.szExeFile;
            m_ProcessList.InsertItem(nItem, str);
            str.Format(_T("%d"), pe32.th32ProcessID);
            m_ProcessList.SetItemText(nItem, 1, str);
            str.Format(_T("%d"), pe32.th32ParentProcessID);
            m_ProcessList.SetItemText(nItem, 2, str);
            str.Format(_T("%d"), pe32.cntThreads);
            m_ProcessList.SetItemText(nItem, 3, str);
            str.Format(_T("%d"), pe32.pcPriClassBase);
            m_ProcessList.SetItemText(nItem, 4, str);
            str = FilePath;
            m_ProcessList.SetItemText(nItem, 5, str);
            nItem++;
        } while (Process32Next(hSnapShot, &pe32));

    } while (FALSE);

    if (INVALID_HANDLE_VALUE != hSnapShot)
        CloseHandle(hSnapShot);
}


void CProcessDlg::OnNMDblclkListProcess(NMHDR* pNMHDR, LRESULT* pResult)
{
    LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);

    int nSel = m_ProcessList.GetSelectionMark();
    if (-1 == nSel) return;

    CString str = m_ProcessList.GetItemText(nSel, 1);
    DWORD dwProcessId = _ttoi(str);

    ::SendMessage(this->GetParent()->m_hWnd, WM_PROCESSID, (WPARAM)dwProcessId, 0);

    EndDialog(0);

    *pResult = 0;
}
